import sys

from PyQt5.QtWidgets import QWidget, QApplication, QFrame, QHBoxLayout
from PyQt5.QtGui import QColor, QFont, QPainter, QBrush, QPen, QCursor
from PyQt5.QtCore import QRect, Qt, QTimer
from PyQt5.QtCore import QObject, pyqtSignal


class MyQFrame(QFrame):
    def __init__(self, parent=None):
        super(MyQFrame, self).__init__(parent)

    # 重写绘制方法
    def paintEvent(self, event):
        super(MyQFrame, self).paintEvent(event)

        # 创建绘制器并设置抗锯齿和图片流畅转换
        painter = QPainter(self)
        painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)

        # 定义字体样式
        font = QFont('Microsoft YaHei')
        font.setPixelSize(40)
        painter.setFont(font)

        # 绘制文本
        pen = QPen(QColor('#ffffff'))
        painter.setPen(pen)
        rect = QRect(0, 0, self.height(), self.height())
        painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter, '😂')


class TestWidget(QWidget):
    def __init__(self):
        super(TestWidget, self).__init__()
        self.resize(70, 70)
        self.frame = MyQFrame()
        # 设置窗口无边框
        self.setWindowFlags(Qt.FramelessWindowHint)
        # 设置窗口透明
        self.setAttribute(Qt.WA_TranslucentBackground)
        # 添加一个布局
        hl = QHBoxLayout()
        hl.setContentsMargins(0, 0, 0, 0)
        self.setLayout(hl)
        hl.addWidget(self.frame)
        self.frame.setStyleSheet('background-color: green; border-radius:30px')

        self.tag = False
        self.timer = QTimer()
        self.timer.timeout.connect(self.check)
        self.timer.start(1000)

    # 重写鼠标按下事件
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标

    # 重写鼠标移动事件
    def mouseMoveEvent(self, event):
        # 如果鼠标按下
        if Qt.LeftButton and self.m_flag:
            self.move(event.globalPos() - self.m_Position)  # 更改窗口位置
            event.accept()

    # 鼠标松开事件
    def mouseReleaseEvent(self, event):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))

    # 鼠标双击时间
    def mouseDoubleClickEvent(self, event):
        print('你双击了我哦')
        self.tag = True

    # 鼠标移入悬停事件
    def enterEvent(self, event):
        print('我又跳进去了')

    def leaveEvent(self, event):
        print('我又跳出来了')

    def check(self):
        print('我一直在等你哦！！！！')
        if self.tag:
            print('你双击了，再见咯')
            self.timer.stop()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = TestWidget()
    widget.show()
    sys.exit(app.exec_())
